<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <h3 class="title">{{ title }}</h3>
      <el-form-item prop="userName">
        <span class="svg-container">
          <i class="el-icon-user"></i>
        </span>
        <el-input v-model="loginForm.userName" class="input-class" name="userName" type="text" auto-complete="on" placeholder="用户名" />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <i class="el-icon-lock"></i>
        </span>
        <el-input
          v-model="loginForm.password"
          :type="pwdType"
          class="input-class"
          name="password"
          auto-complete="on"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="pwdType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <el-form-item prop="isChecked" class="no-class">
        <el-checkbox v-model="isChecked" @change="changeValue">自动登录</el-checkbox>
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" type="primary" style="width: 100%" @click.native.prevent="handleLogin">登录</el-button>
      </el-form-item>
      <div v-if="isDebug" class="tips">
        <span style="margin-right: 20px">用户名: {{ loginForm.userName }}</span>
        <span>密码: {{ loginForm.password }}</span>
      </div>
      <!-- <div class="forget-password">
				<router-link to="/forgetPwd">忘记密码</router-link>
      </div>-->
    </el-form>
    <div class="footer">
      <!-- <div>Copyright © 2021 深圳市 xxxx 有限公司 版权所有</div> -->
      <a target="_blank" href="http://beian.miit.gov.cn">粤ICP备19007446号-2</a>
    </div>
  </div>
</template>

<script>
import utils from 'utils'
import { root } from '@/utils/constants'

export default {
  name: 'login',
  data() {
    return {
      title: process.env.VUE_APP_TITLE,
      loginForm: {
        userName: utils.isDebug ? 'admin' : '',
        password: utils.isDebug ? 'yccl123@' : ''
      },
      loginRules: {
        userName: [
          {
            required: true,
            trigger: 'blur',
            validator: utils.validater.validateUserName
          }
        ],
        password: [{ required: true, trigger: 'blur', validator: utils.validater.validatePass }]
      },
      loading: false,
      pwdType: 'password',
      redirect: undefined,
      isDebug: utils.isDebug,
      isChecked: false
    }
  },
  mounted() {
    if (utils.getCookie('userName') && utils.getCookie('password')) {
      this.loginForm.userName = utils.getCookie('userName')
      this.loginForm.password = utils.getCookie('password')
      this.isChecked = true
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    changeValue(value) {
      if (!value) {
        utils.delCookie('userName')
        utils.delCookie('password')
      }
    },
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = 'text'
      } else {
        this.pwdType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          let password = ''
          if (this.isChecked) {
            if (utils.getCookie('password') !== this.loginForm.password) {
              password = this.loginForm.password
            } else {
              password = utils.getCookie('password')
            }
          } else {
            password = this.loginForm.password
          }
          this.$store
            .dispatch('user/login', { ...this.loginForm, password })
            .then(res => {
              this.loading = false
              if (this.isChecked) {
                utils.setCookie('userName', this.loginForm.userName, 30) // 保存帐号到cookie，有效期7天
                utils.setCookie('password', this.loginForm.password, 30) // 保存密码到cookie，有效期7天
              }
              const cityInfo = {
                cityCode: res.cityCode,
                cityName: res.cityName
              }
              utils.setStore('cityInfo', cityInfo)
              this.$store.dispatch('views/getAllCity').then(cityList => {
                const curCity = cityList.find(item => item.value === res.cityCode)
                if (curCity || res.cityCode === '00') {
                  utils.setStore('cityInfo', {
                    cityCode: res.cityCode,
                    cityName: res.cityName,
                    companyId: res.companyId || '',
                    longitude: curCity ? curCity.longitude : '113.9725112915', // 默认深圳经纬度
                    latitude: curCity ? curCity.latitude : '22.5410984118'
                  })
                }
                this.$router.push({ path: this.redirect || '/' })
              })
              if (res.roles) {
                const isRoot = res.roles.some(role => role.val === root.rval)
                isRoot && this.$store.dispatch('views/getAllCompany')
              }
            })
            .catch(err => {
              this.$msgTip(`${err.msg || '系统异常，请重新'}【${err.code || '未知'}】`, 'warning')
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="less">
/* reset element-ui css */
.login-container {
  .input-class {
    display: inline-block;
    width: 85%;
    height: 47px;
    input {
      height: 47px;
      padding: 12px 5px 12px 15px;
      color: @light_gray;
      background: transparent;
      border: 0px;
      border-radius: 0px;
      -webkit-appearance: none;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px @login-bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
  }
  .el-form-item {
    color: #454545;
    background: rgba(0, 0, 0, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 5px;
  }
  .el-checkbox__inner {
    background-color: @light_gray;
  }
  .el-checkbox__label {
    font-size: @f12;
    color: @light_gray;
  }
}
</style>

<style lang="less" scoped>
.login-container {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: @login-bg;

  .login-form {
    position: absolute;
    right: 0;
    left: 0;
    width: 520px;
    max-width: 100%;
    padding: 35px 35px 15px 35px;
    margin: 120px auto;
    .no-class {
      margin: -20px 0 0 2px;
      margin-bottom: 0rem;
      background: none;
      border: none;
    }
    .forget-password {
      color: @light_gray;
      text-align: center;
    }
  }
  .tips {
    margin-bottom: 10px;
    font-size: @f14;
    color: @light_gray;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    display: inline-block;
    width: 30px;
    padding: 6px 5px 6px 15px;
    color: @dark_gray;
    vertical-align: middle;
    i {
      font-size: 18px;
    }
  }
  .title {
    margin: 0px auto 40px auto;
    font-size: 26px;
    font-weight: 400;
    font-weight: bold;
    color: @light_gray;
    text-align: center;
  }
  .show-pwd {
    position: absolute;
    top: 7px;
    right: 10px;
    font-size: @f16;
    color: @dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .footer {
    position: absolute;
    bottom: 20px;
    width: 100%;
    font-size: @f12;
    color: @light_gray;
    text-align: center;
    a {
      color: @light_gray;
      &:hover {
        color: @primary-color;
      }
    }
  }
}
</style>
